//
//  FeedBackViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/8.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "FeedBackViewController.h"

@interface FeedBackViewController ()
<UITextViewDelegate,UIAlertViewDelegate>
@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    [self addRightItem];
    self.navigationItem.title = @"意见反馈";
    
   // [self addBackItem];
    //[self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    self.view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64+SIZETO6H(18), SCREEN_WIDTH, 150)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [self.view addSubview:view];
    
    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6H(14), view.width-SIZETO6W(15)*2, 150-SIZETO6H(14)*2)];
    textView.delegate = self;
    textView.backgroundColor = [UIColor redColor];
    [view addSubview:textView];
    
    UITextView * textV = [[UITextView alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6H(14), view.width-SIZETO6W(15)*2, 150-SIZETO6H(14)*2)];
    textV.backgroundColor = [UIColor whiteColor];
    textV.delegate = self;
    textV.tag = 712;
    [view addSubview:textV];
    
    UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15)+10, SIZETO6H(14), 120, 30)];
    tipLab.text = @"感谢反馈...";
    tipLab.font = [UIFont systemFontOfSize:15];
    tipLab.textColor = UIColorFromRGB(0x94999f);
    tipLab.tag = 1;
    [view addSubview:tipLab];
    
    UILabel * countLab = [[UILabel alloc]initWithFrame:CGRectMake(view.right - SIZETO6W(15) -80, view.bottom+SIZETO6H(8), 80, 20)];
    countLab.textColor = UIColorFromRGB(0x94999f);
    countLab.text = @"0/200";
    countLab.tag = 2;
    countLab.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:countLab];
}
-(void)submitAdviceWithContent:(NSString *)content
{
    __weak typeof(self)weakSelf = self;
    [U76HONet adviceFeedBackWithAction:@"" code:[Singleton share].code content:content andDataBlock:^(NSDictionary *dic) {
        NSLog(@"提交建议反馈成功列");
        [Singleton alerSendMessage:@"感谢您的反馈"];
        [weakSelf performSelector:@selector(backItem:) withObject:nil afterDelay:1.0];
    }];
}
#pragma mark - textViewDelegate
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.tag == 712) {
        UILabel * lab = (UILabel *)[self.view viewWithTag:1];
        if (textView.text.length == 0) {
            lab.hidden = NO;
        }
        else
        {
            lab.hidden = YES;
        }
        UILabel * countLab = (UILabel *)[self.view viewWithTag:2];
        countLab.text = [NSString stringWithFormat:@"%i/200",(int)textView.text.length];

    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - addRightItem
-(void)addRightItem
{
    UIButton * subBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    subBtn.frame = CGRectMake(0, 0, 50, 30);
    [subBtn setTitle:@"提交" forState:UIControlStateNormal];
    [subBtn addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    [subBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:subBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
-(void)subAction:(UIButton *)btn
{
    if ([self isLogin]) {
        UITextView * textView = (UITextView *)[self.view viewWithTag:712];
        if (![textView.text isEqualToString:@""]) {
            [self submitAdviceWithContent:textView.text];
        }
    }
    
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {

        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
